package com.qst.controller;// UserController.java

import com.qst.domain.User;
import com.qst.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
public class UserController {
    // 自动从IOC容器中找到对应的bean,并注入到该变量中
    @Autowired
    private UserService userService;

    /*
    用户登录
    */
    @RequestMapping("/login")
    public String login(User user, HttpServletRequest request){
        User u = userService.login(user);
        /*
        用户账号和密码是否查询出用户信息
        是:将用户信息存入session,并跳转到后台首页
        否:Request域中添加提示信息,并转发到登录页面
        */
        if (u!=null){
            request.getSession().setAttribute("USER_SESSION",u);
            return "redirect:/admin/main.jsp";
        }
        request.setAttribute("msg","用户名或密码错误");
        return "forward:/admin/login.jsp";
    }

    /*
   跳转到后台系统首页的方法.
   */
    @RequestMapping("/toMainPage")
    public String toMainPage(){
        return "main";
    }

    /*
    用户注销
     */
    @RequestMapping("/logout")
    public String logout(HttpServletRequest request){
        // 对session进行销毁
        request.getSession().invalidate();
        return "login";
    }
}